package com.reggie.console.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reggie.acl.dataobject.Employee;
import com.reggie.common.enums.ResponseCode;
import com.reggie.common.exception.ReggieException;
import com.reggie.common.model.PageDTO;
import com.reggie.common.util.ReggieContext;
import com.reggie.console.annotation.AuthRule;
import com.reggie.console.convertor.EmployeeConvertor;
import com.reggie.console.convertor.PageDTOConvertor;
import com.reggie.console.dto.EmployeeDTO;
import com.reggie.console.dto.EmployeeDetailDTO;
import com.reggie.console.dto.ResultDTO;
import com.reggie.console.enums.ReggieRole;
import com.reggie.console.util.ResultBuilder;
import com.reggie.service.model.request.EmployeeLoginRequest;
import com.reggie.service.model.request.EmployeePageRequest;
import com.reggie.service.model.request.EmployeeSaveRequest;
import com.reggie.service.model.request.EmployeeUpdateRequest;
import com.reggie.service.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * <p>
 * 员工信息 前端控制器
 * </p>
 *
 * @author yth
 * @since 2023-02-04
 */
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    private static final String EMPLOYEE_SESSION_KEY = "employee";

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private EmployeeConvertor employeeConvertor;

    /**
     * 员工登入
     * 登入则存储Session
     *
     * @param employeeLoginRequest 员工的用户名和密码
     * @return 消息
     */
    @PostMapping("/login")
    public ResultDTO<EmployeeDTO> login(HttpServletRequest request, @RequestBody EmployeeLoginRequest employeeLoginRequest) {
        log.info("login()");
        Employee loginEmp = employeeService.login(employeeLoginRequest);
        // 登入成功
        request.getSession().setAttribute(EMPLOYEE_SESSION_KEY, loginEmp.getId());
        EmployeeDTO dto = employeeConvertor.toDTO(loginEmp);
        return ResultBuilder.ok(dto);
    }

    /**
     * 员工登出
     *
     * @return 消息
     */
    @PostMapping("/logout")
    @AuthRule(roles = {ReggieRole.EMPLOYEE})
    public ResultDTO<String> logout(HttpServletRequest request) {
        request.getSession().removeAttribute(EMPLOYEE_SESSION_KEY);
        return ResultBuilder.ok("登出成功");
    }

    /**
     * 根据昵称，分页查询员工数据
     *
     * @param employeePageRequest 查询条件
     * @return 员工分页数据
     */
    @GetMapping("/page")
    @AuthRule(roles = {ReggieRole.EMPLOYEE})
    public ResultDTO<PageDTO<EmployeeDTO>> page(EmployeePageRequest employeePageRequest) {
        Page<Employee> pageInfo = employeeService.page(employeePageRequest);
        PageDTO<EmployeeDTO> pageDTO = PageDTOConvertor.convertModelToPageDTO(
                pageInfo, employeeConvertor::toDTO
        );
        return ResultBuilder.ok(pageDTO);
    }

    /**
     * 根据id查询员工个人信息
     *
     * @param id 员工id
     * @return 员工数据
     */
    @GetMapping("/{id}")
    @AuthRule(roles = {ReggieRole.EMPLOYEE})
    public ResultDTO<EmployeeDetailDTO> getById(@PathVariable long id) {
        // 员工自己只能查看自己的信息，管理员可以查看任何员工的信息
        Long employeeId = ReggieContext.getInstance().getEmployeeId();
        if (!Objects.equals(employeeId, 1L) && !Objects.equals(employeeId, id)) {
            throw new ReggieException(ResponseCode.CLIENT_ERROR, "未授权操作");
        }
        // 查询
        Employee employee = employeeService.getById(id).orElse(null);
        if (employee == null) {
            return ResultBuilder.emptyPojoResult();
        }
        EmployeeDetailDTO dto = employeeConvertor.toDetailDTO(employee);
        return ResultBuilder.ok(dto);
    }

    /**
     * 添加员工
     *
     * @param request 员工信息
     * @return 消息
     */
    @PostMapping
    @AuthRule(roles = {ReggieRole.MANAGER})
    public ResultDTO<String> save(@RequestBody EmployeeSaveRequest request) {
        employeeService.saveEmployee(request);
        return ResultBuilder.ok("添加员工成功");
    }

    /**
     * 修改员工信息
     *
     * @param request 修改的员工数据
     * @return 消息
     */
    @PutMapping
    @AuthRule(roles = {ReggieRole.EMPLOYEE})
    public ResultDTO<String> update(@RequestBody EmployeeUpdateRequest request) {
        // 员工自己只能改自己的信息，管理员可以改任何员工的信息
        Long employeeId = ReggieContext.getInstance().getEmployeeId();
        if (!Objects.equals(employeeId, 1L) && !Objects.equals(employeeId, request.getId())) {
            throw new ReggieException(ResponseCode.CLIENT_ERROR, "未授权操作");
        }

        employeeService.update(request);
        return ResultBuilder.ok("修改员工信息成功");
    }

}

